package com.boot_reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.boot_reggie.Service.impl.Dishserviceimpl;
import com.boot_reggie.Service.impl.SetmealDishServiceimpl;
import com.boot_reggie.Service.impl.SetmealServiceimpl;
import com.boot_reggie.common.Result;
import com.boot_reggie.domain.Dish;
import com.boot_reggie.domain.Setmeal;
import com.boot_reggie.domain.SetmealDto;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.sql.SQLIntegrityConstraintViolationException;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/Packages")
public class SetmealController {
    @Autowired
    private SetmealServiceimpl setmealServiceimpl;
    @Autowired
    private SetmealDishServiceimpl setmealDishServiceimpl;

    @Autowired
    private Dishserviceimpl dishserviceimpl;

    @GetMapping("list")
    public Result<List<Dish>> list (Dish dish){
//        构造查询
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(dish.getCategoryId() != null,Dish::getCategoryId,dish.getCategoryId());
        queryWrapper.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);
        List<Dish> list  = dishserviceimpl.list(queryWrapper);
        return Result.success(list,"查询成功");
    }
//提交套餐
    @PostMapping
    public Result<String> save(@RequestBody SetmealDto setmealDto){
        System.out.println(setmealDto);
        setmealServiceimpl.saves(setmealDto);
        return Result.success("添加套餐成功");
    }
//    分页
    @GetMapping
    public Result<Page<SetmealDto>> getAll(int page,int pageSize,String name){
        Page<SetmealDto> dtoPage = setmealServiceimpl.pages(page,pageSize,name);
        if(page > dtoPage.getPages()){
            dtoPage = setmealServiceimpl.pages((int)dtoPage.getPages(),pageSize,name);
        }


        return Result.success(dtoPage,"获取成功");
    }
//    批量删除
    @DeleteMapping
    public Result<String> delete (@RequestParam List<Long> ids){
        setmealServiceimpl.removeWithDish(ids);
        return Result.success("删除成功");
    }
//    批量修改接口
    @PutMapping()
    public Result<String> update( @RequestParam int status,@RequestParam List<Long> ids){
//        System.out.println(ids);
//        System.out.println(status);
        if(!Strings.isNotEmpty(String.valueOf(status))){
            return Result.error("status参数错误");
        }
        try{
            setmealServiceimpl.updateWithSetmeal(status,ids);

        }catch (Exception ex){
            return Result.error("后端接口错误");
        }
        return Result.success("修改成功");
    }
//获取套餐接口
    @GetMapping("Setmeal/{id}")
    public Result<SetmealDto> setMealDto(@PathVariable Long id){
        System.out.println(id);
        if(String.valueOf(id) == null){
            return Result.error("参数不能为空");
        }
        return Result.success(setmealServiceimpl.selectID(id),"数据查询成功");
    }
    @PostMapping("Setmeal")
    public Result<String> saveDate(@RequestBody SetmealDto setmealDto){
        setmealServiceimpl.updateDate(setmealDto);

        return Result.success("更新成功");
    }
}
